//app.js
App({
  onLaunch: function () {
    
    var that = this;

    wx.login({
      success: function (res) {
        that.globalData.loginCode = res.code
      }
    });

    //获取用户电话
    wx.request({
      url: that.globalData.surl +'config/get-value',
      data:{
        key: 'phoneNum',
        jkum: that.globalData.jkum
      },
      success:function(res){
        wx.setStorageSync('phoneNum', res.data.value);
      }
    })

    //  获取商城名称
    wx.request({
      url: that.globalData.surl +'config/get-value',
      data: {
        key: 'mallName',
        jkum : that.globalData.jkum
      },
      success: function(res) {
        wx.setStorageSync('mallName', res.data.value);
      }
    })
    //获取位置经度(获取地址：http://lbs.qq.com/tool/getpoint/)
    wx.request({
      url: that.globalData.surl + 'config/get-value',
      data: {
        key: 'latitude',
        jkum: that.globalData.jkum
      },
      success: function (res) {
        wx.setStorageSync('latitude', res.data.value);
      }
    })
    //获取位置纬度
    wx.request({
      url: that.globalData.surl + 'config/get-value',
      data: {
        key: 'longitude',
        jkum: that.globalData.jkum
      },
      success: function (res) {
        wx.setStorageSync('longitude', res.data.value);
      }
    })

  },
  getUserInfo: function (cb) {
    this.globalData.userInfo = cb.userInfo
    this.globalData.iv = cb.iv
    this.globalData.encryptedData = cb.encryptedData

  },
  saveOpenId: function (data) {

    var that = this;
    wx.request({
      url: that.globalData.surl + 'wx-manage/saveUser',
      data: {
        openId: data.openId,
        unionId: data.unionId,
        nickname: data.nickName
      },
      success : function(res){
          if(!res.data.ok){
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              success: function (res) {
                if (res.confirm) {
                  wx.redirectTo({
                    url: 'pages/start/start',
                  })
                } else if (res.cancel) {
                  wx.redirectTo({
                    url: 'pages/start/start',
                  })
                }
              }
            })
          }
      },
      fail : function(res){
        wx.redirectTo({
          url: 'pages/start/start',
        })
      }
    });
    this.globalData.openId = data.openId;
    this.globalData.nickname = data.nickName;
  },

  globalData:{
    userInfo:null,
    subDomain:"34vu54u7vuiuvc546d",
    url: 'https://api.it120.cc/jkum',
    surl: 'https://jk.guyubao.com/',
    jkum : 'jk',
    appid: 'wx3952810f7390a618',
    appsecret: '9fb9cafe20f3bbfb69f2be2d6677bbed',
    template_id: 'PPrU3QqEd_Dkl7t7gauCPWANoiH5BQVBVAWi5LW_bdw',
    loginCode: null,
    encryptedData: null,
    iv: null,
    openId: null,
    nickname: null
  }
})